Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Java Datatypes

Long in Java

What is Long in Java

The long datatype is a signed 64-bit integer type. This means that it can store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Long variables are declared by using the long keyword. For example,
Java long declaration
long a, b; long c = 2324324; long d = 'dgfdg'; //wrong declaration long e = 34434245455L; // Make sure to add 'L' or 'l' to indicate it's a long literal long f = 124324.454; //wrong declaration (float/double)
Long variables are often used to store data that is outside the range of the int datatype especially for a long values. They are also useful when working with data that is not as precise as floating-point data. Here is a more complex example of how to use the long datatype:
java Long datatype example
public class Main{ public static void main(String[] args) throws Exception { long money = 1000000; long dividedMoney = money / 100; System.out.println("The money is " + money); System.out.println("The money per person is " + dividedMoney); money += 500000; dividedMoney = money / 100; System.out.println("The money is now " + money); System.out.println("The money person is now " + dividedMoney); } }

Output

The money is 1000000 The money per person is 10000 The money is now 1500000 The money person is now 15000
In this example, we declare a long variable called money and initialize it to the value 1000000. We then calculate the money divided by dividing the money by 100. We print the value of the money and the money divided to the console. Next, we add 500000 to the value of the money variable. We then recalculate the money divided and print the new value to the console. Here are some key points to understand about the long datatype: Range: The long datatype has a larger range than the int datatype, making it suitable for storing very large integer values. Memory: As a 64-bit datatype, a long variable occupies more memory compared to an int variable, which is a 32-bit datatype. Operations: You can perform various arithmetic and logical operations on long values, similar to other numeric datatypes. Type Casting: When performing operations involving long and other numeric types (such as int), you might need to explicitly cast the values to avoid potential data loss. Use Cases: The long datatype is often used in scenarios where precision and a wide range of integer values are required, such as timestamp representations, calculations involving large numbers, and situations where the int range is insufficient.

  📌TAGS

★long in Java ★long datatype ★Java long ★datatype ★Integer ★Java integer ★Java long size

Tutorials